package sjq.com.title;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import sjq.com.title.refresh.RefreshFooter;
import sjq.com.title.refresh.RefreshHeader;
import sjq.com.title.refresh.SpringView;

public class MainActivity extends Activity implements MyScrollView.ScrollViewListener{

    private MyScrollView scrollView;
    private SpringView springView;
    private RelativeLayout rl, rl2;
    private LinearLayout ll;
    private ImageView img;

    private int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        setView(R.layout.activity_main, "type_background");

        setView();

        initListeners();

        springView.setHeader(new RefreshHeader(this,true)); //设置头部 ,是否显示文字, 可以设置背景图等等
        springView.setType(SpringView.Type.FOLLOW);  //设置下拉的类型
        springView.setFooter(new RefreshFooter(this,true)); //设置foot , 是否显示文字, 也可设置背景图
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }
            @Override
            public void onLoadmore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }
        });
    }

    private void setView(){

        scrollView = (MyScrollView) findViewById(R.id.scrollView);
        springView = (SpringView) findViewById(R.id.springview);
        rl = (RelativeLayout) findViewById(R.id.rl);
        rl2 = (RelativeLayout) findViewById(R.id.rl2);
        img = (ImageView) findViewById(R.id.image);

        //定义一个LayoutParams
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.height = 100;

        //Android4.4以上
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){

            //设置导航栏总体高度
            RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layout.height = 100 + getStatusBarHeight();
            rl2.setLayoutParams(layout);

            layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);

            Window window = getWindow();
            ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);

            //Android5.0以上
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                //设置透明状态栏,这样才能让 ContentView 向上
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                //设置状态栏颜色
                window.setStatusBarColor(Color.TRANSPARENT);

                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null) {
                    //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
                    ViewCompat.setFitsSystemWindows(mChildView, false);
                }
            }else{
                //Android4.4到5.0之间
                //首先使 ChildView 不预留空间
                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null) {
                    ViewCompat.setFitsSystemWindows(mChildView, false);
                }
                int statusBarHeight = getStatusBarHeight();
                //需要设置这个 flag 才能设置状态栏
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //避免多次调用该方法时,多次移除了 View
                if (mChildView != null && mChildView.getLayoutParams() != null && mChildView.getLayoutParams().height == statusBarHeight) {
                    //移除假的 View.
                    mContentView.removeView(mChildView);
                    mChildView = mContentView.getChildAt(0);
                }
                if (mChildView != null) {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
                    //清除 ChildView 的 marginTop 属性
                    if (lp != null && lp.topMargin >= statusBarHeight) {
                        lp.topMargin -= statusBarHeight;
                        mChildView.setLayoutParams(lp);
                    }
                }
            }
        }

        rl.setLayoutParams(layoutParams);
    }

    /**
     * 获取状态栏高度
     * @return
     */
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    private void initListeners() {
        // 获取顶部图片高度后，设置滚动监听
        ViewTreeObserver vto = img.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                img.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
        height = img.getHeight();
        scrollView.setScrollViewListener(MainActivity.this);
            }
        });
    }

    //scrollview的滚动监听
    @Override
    public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {
            rl2.setBackgroundColor(Color.argb((int) 0, 51, 143, 255));
        } else if (y > 0 && y <= height) {
            float scale = (float) y / height;
            float alpha = (255 * scale);
            rl2.setBackgroundColor(Color.argb((int) alpha, 51, 143, 255));
        } else {
            rl2.setBackgroundColor(Color.argb((int) 255, 51, 143, 255));
        }
    }
}
